//
//  StudyLeftView.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/3/29.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "StudyLeftView.h"
#import "studyCollectionCell.h"
#import "niceCollectionCell.h"
#import "BaseCollectionCell.h"
#import "StudyInforListVC.h"

@interface StudyLeftView ()
<UICollectionViewDataSource,UICollectionViewDelegate>

@end

@implementation StudyLeftView
{
    RequestServices *service;
    NSInteger page_index;//页码
    NSMutableArray *list_arr;//列表数组
    NSMutableArray *bottomArr;
    BOOL showRound;//显示圆
}

-(void)addView{
    [self setUI];
    showRound = NO;
    list_arr = [[NSMutableArray alloc]init];
    service = [[RequestServices alloc]initWithDelegate:self];
    [self headerRefresh];
}

-(void)setUI{
    self.delegate = self;
    self.dataSource = self;
    self.backgroundColor = [UIColor clearColor];
    bottomArr = [NSMutableArray array];
    [self registerNib:[UINib nibWithNibName:@"studyCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"studyCollectionCell"];
    [self registerNib:[UINib nibWithNibName:@"niceCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"niceCollectionCell"];
    [self registerNib:[UINib nibWithNibName:@"BaseCollectionCell" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BaseCollectionCell"];
}

- (void)headerRefresh
{   
    [service getClassList:[NSString stringWithFormat:@"%ld",page_index] tag:8000];
}

#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self.VC HideLoading];
    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:obj[@"list"]];
    
    if (tag == 8000) {
        if ([tempArr CheckNSArrayData]) {
            list_arr = [StudyListModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
            
            if (list_arr.count > 3) {
                [list_arr removeObjectsInRange:NSMakeRange(3, list_arr.count - 3)];
            }
            [self reloadData];
        }
        service.view = self;
        [service getCurriculumList:[NSString stringWithFormat:@"%ld",page_index] categoryId:@"0" recommend:@"1" stageId:@"" tag:8010];
    }
    //最多显示三个（湖南产品设计nc）
    if (tag == 8010) {
        bottomArr = [CurriculumListModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
        if (bottomArr.count > 3) {
            [bottomArr removeObjectsInRange:NSMakeRange(3, bottomArr.count - 3)];
        }
        showRound = YES;
        [self reloadData];
    }
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self.VC HideLoading ];
    [self.VC ShowNewMsg:error.domain];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return list_arr.count;
    }else{
        return bottomArr.count;
    }
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake((collectionView.frame.size.width - 60)/3, (collectionView.frame.size.height)*0.4);
    }else{
        return CGSizeMake((collectionView.frame.size.width - 100)/3, (collectionView.frame.size.height)*0.25);
    }
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        studyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"studyCollectionCell" forIndexPath:indexPath];
        if (showRound == YES) {
            cell.model = list_arr[indexPath.item];            
        }
        return cell;
    }else{
        niceCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"niceCollectionCell" forIndexPath:indexPath];
        cell.model = bottomArr[indexPath.item];
        return cell;
    }
}

//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

//每个section的header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    BaseCollectionCell *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BaseCollectionCell" forIndexPath:indexPath];
    view.base_lab.textColor = [UIColor whiteColor];
    view.base_lab.font = AutoLabFont2;
    if (indexPath.section == 0) {
         view.base_lab.text = @"";
    }
    else {
        if (bottomArr.count>0) {
            view.base_lab.text = @"   精彩课程";
        }
    }
    return view;
}

//header的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
        return CGSizeMake(collectionView.frame.size.width, collectionView.frame.size.height*0.15);
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        StudyInforListVC *vc = [[StudyInforListVC alloc]init];
        StudyListModel *model = list_arr[indexPath.row];
        vc.id = model.id;
        vc.name = [NSString stringWithFormat:@"学习中心-%@",model.categoryName];
        [self.VC PushToViewController:vc];
    }
    
    if (indexPath.section == 1) {
        
    }
}

- (UIEdgeInsets) collectionView:(UICollectionView *)collectionView
    layout:(UICollectionViewLayout *)collectionViewLayout
    insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0.0f, 20.0f, 0.0f, 20.0f);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
